package iotwisp.school.api.controller.pc_admin.user;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.entity.Permission;
import iotwisp.school.user.qo.PermissionQO;
import iotwisp.school.user.service.PermissionService;

@RestController
@RequestMapping("/api/org/permission")
public class PermissionController extends RestBaseController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<JSONResultDTO> queryPermission(@RequestBody(required = false) PermissionQO qoBody, @RequestParam(name = "qo", required = false) String qoJson)
            throws UnsupportedEncodingException {

        PermissionQO qo;
        if (!StringUtils.isBlank(qoJson)) {
            qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), PermissionQO.class);
        } else {
            qo = qoBody;
        }

        switch (qo.getResultType()) {
            case PermissionQO.RESULT_TYPE_COUNT:
                Integer count = permissionService.queryCount(qo);
                return ResponseEntity.ok(createSuccessResult(count));
            case PermissionQO.RESULT_TYPE_UNIQUE:
                Permission permission = permissionService.queryUnique(qo);
                return ResponseEntity.ok(createSuccessResult(permission));
            case PermissionQO.RESULT_TYPE_LIST:
                List<Permission> permissions = permissionService.queryList(qo);
                if (qo.getReturnTree() != null && qo.getReturnTree()) {
                    permissions = transToTree(permissions);
                }
                return ResponseEntity.ok(createSuccessResult(permissions));
            case PermissionQO.RESULT_TYPE_PAGINATION:
                Pagination pagination = permissionService.queryPagination(qo);
                return ResponseEntity.ok(createSuccessResult(pagination));
            default:
                return ResponseEntity.badRequest()
                        .body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
        }

    }

    private List<Permission> transToTree(List<Permission> permissions) {

        List<Permission> list = new ArrayList<>();
        Map<String, List<Permission>> permissionChildren = new HashMap<>();

        for (Permission permission : permissions) {
            if (!StringUtils.isBlank(permission.getTreeData().getParentId())) {
                if (!permissionChildren.containsKey(permission.getTreeData().getParentId())) {
                    permissionChildren.put(permission.getTreeData().getParentId(), new ArrayList<>());
                }
                permissionChildren.get(permission.getTreeData().getParentId()).add(permission);
            }
        }

        for (Permission permission : permissions) {
            if (StringUtils.isBlank(permission.getTreeData().getParentId())) {
                permission.setChildren(permissionChildren.get(permission.getId()));
                list.add(permission);
            }
        }
        return list;
    }

}
